<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/dao/GenericDAO.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/dao/JovemDAO.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/dao/UsuarioDAO.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/Usuario.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/Jovem.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/utils/System.php'; ?>

<?php

/**
 * Description of InscricaoDAO
 *
 * @author JoasRamos
 */
class InscricaoDAO extends GenericDAO {

    public function __construct() {
        parent::__construct();
    }

    function insertFirst(Inscricao $i, $valores) {
        $count = 0;
        foreach ($valores as $valor) {
            if (!is_numeric($valor)) {
                $valor = "'" . $valor . "'";
                $valores[$count] = $valor;
            }
            $count++;
        }
        $sql = "INSERT INTO " . $i . " VALUES (" . implode(', ', $valores) . ")";
        echo $sql;
        $result = mysql_query($sql);
        mysql_query("ALTER TABLE inscricao AUTO_INCREMENT=" . $i->getId());
        if ($result) {
            return true;
        }
        return false;
    }

    function insertNext(Inscricao $i, $valores) {
        $count = 0;
        $campos = $i->getAttributes();
        foreach ($valores as $valor) {
            if (!is_numeric($valor)) {
                $valor = "'" . $valor . "'";
                $valores[$count] = $valor;
            }
            $count++;
        }
        $sql = "INSERT INTO " . $i . " (" . implode(', ', $campos) . ") " . " 
            VALUES (" . implode(', ', $valores) . ")";
        echo $sql;
        $result = mysql_query($sql);
        if ($result) {
            return true;
        }
        return false;
    }

    function findAtivas($id) {
        $list = array();
        $registro = 0;

        $sql = "select *,i.id as idInscricao, u.nome as nomeUsuario, j.nome as nomeJovem 
            from jovem j, inscricao i, usuario u 
            where j.id=i.jovem  
            AND u.id=i.usuario 
            AND i.status=1 
            AND i.id like '%" . $id . "%'";

        $result = mysql_query($sql);

        if ($result) {
            while ($row = mysql_fetch_assoc($result)) {
                $list[$registro] = $row;
                $registro++;
            }
        }

        return $list;
    }

    function filterBy($campo, $value) {
        $list = array();
        $registro = 0;

        if (!is_numeric($value)) {
            $value = "'" . $value . "'";
        }
        $sql = "select *,i.id as idInscricao, u.nome as nomeUsuario, j.nome as nomeJovem 
            from jovem j, inscricao i, usuario u 
            where j.id=i.jovem  
            AND u.id=i.usuario 
            AND i.status=1 
            AND j." . $campo . "=" . $value;

        $result = mysql_query($sql);

        if ($result) {
            while ($row = mysql_fetch_assoc($result)) {
                $list[$registro] = $row;
                $registro++;
            }
        }

        return $list;
    }

}
?>
